package com.gw.controller;

import com.gw.context.BaseContext;
import com.gw.pojo.dto.UserUpdateDTO;
import com.gw.pojo.entity.User;
import com.gw.pojo.vo.UserVO;
import com.gw.result.PageResult;
import com.gw.result.Result;
import com.gw.service.PersonalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/personal")
public class PersonalController {

    @Autowired
    private PersonalService personalService;
    @Autowired
    private ServerProperties serverProperties;

    /**
     * 修改个人信息
     * @param userUpdateDTO
     * @return
     */
    @PutMapping("/update")
    public Result updatePersonal(@RequestBody @Validated UserUpdateDTO userUpdateDTO,
                                               BindingResult bindingResul) {
        if (bindingResul.hasErrors()) {
            throw new RuntimeException("修改个人信息时上传信息有误");
        }
        personalService.updatePersonal(userUpdateDTO);
        return Result.success();
    }

    /**
     * 查询用户信息
     * @return
     */
    @GetMapping("/getUserDetail")
    public Result<UserVO> getUserDetail(){
        UserVO userVO = personalService.getUserDetail();
        return Result.success(userVO);
    }

    /**
     * 查询我的帖子
     */
    @GetMapping("/getMyList")
    public Result<PageResult> listMyPost(int page, int size){
        PageResult pageResult = personalService.pageMyPost(page, size);
        return Result.success(pageResult);
    }

    @DeleteMapping("/delete/{id}")
    public Result<Integer> deletePost(@PathVariable("id") Long id) {
        //Long userId = BaseContext.getCurrentUserId();

        //TODO: 后面改为获取的当前登录用户的id
        int result = personalService.deleteMyPost(id, 2L);
        return Result.success(result);
    }
}
